package com.jiyici.fragment;

import android.graphics.Point;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.jiyici.DragSortController;
import com.jiyici.DragSortListView;
import com.jiyici.DragSortListView.DropListener;
import com.jiyici.R;
import com.jiyici.controller.DataManager;
import com.jiyici.controller.DataManager.DataListener;
import com.jiyici.model.Category;
import com.jiyici.view.DialogUtil;
import com.jiyici.view.DialogWrapper.DialogAction;

public class CategoryListFragment extends BaseFragment implements DataListener {

    private DragSortListView mCategoryList;

    private CategoryDragSortAdapter mCategoryDragSortAdapter = new CategoryDragSortAdapter();

    private LayoutInflater mLayoutInflater;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.add(R.string.actionbar_item_new_category).setIcon(R.drawable.ic_content_new)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (getText(R.string.actionbar_item_new_category).equals(item.getTitle())) {
            DialogUtil.showNewCategoryDialog(getActivity());
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_category_list, container, false);
        mCategoryList = (DragSortListView) view.findViewById(R.id.category_list);
        mCategoryList.setAdapter(mCategoryDragSortAdapter);
        mCategoryList.setDropListener(mCategoryDragSortAdapter);
        mCategoryList.setDragEnabled(true);
        DragController dc = new DragController(mCategoryList, mCategoryDragSortAdapter);
        mCategoryList.setFloatViewManager(dc);
        

        mLayoutInflater = LayoutInflater.from(getActivity());
        return view;
    }

    @Override
    public void onItemCatgoryChanged() {
        mCategoryDragSortAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRecordChanged() {
        // NO-OP
    }



    private final class CategoryDragSortAdapter extends BaseAdapter implements DropListener {

        @Override
        public int getCount() {
            return DataManager.getCategoryList().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final Category category = DataManager.getCategoryList().get(position);
            View view = mLayoutInflater.inflate(R.layout.category_row2, parent, false);
            TextView categoryRow = (TextView) view.findViewById(R.id.txt_name);
            categoryRow.setText(category.name);
            view.findViewById(R.id.btn_delete_category).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String title = getString(R.string.delete) + ": \"" + category.name + "\"";
                    DialogAction doneAction = new DialogAction() {

                        @Override
                        public int run() {
                            DataManager.deleteCategory(category);
                            return DialogAction.DISMISS;
                        }
                    };
                    DialogUtil.showConfirmDialog(getActivity(), title,
                            getString(R.string.dialog_confirm_delete_category), doneAction);

                }
            });
            return view;
        }

        @Override
        public void drop(int from, int to) {
            if (from != to) {
                DataManager.changeOrderForCategory(from, to);
                notifyDataSetChanged();
            }
        }

    }
    
    private class DragController extends DragSortController {
        
        CategoryDragSortAdapter mAdapter;
        DragSortListView mDslv;

        public DragController(DragSortListView dslv, CategoryDragSortAdapter adapter) {
            super(dslv);
            mAdapter = adapter;
            mDslv = dslv;
        }


        @Override
        public View onCreateFloatView(int position) {
            View v = mAdapter.getView(position, null, mDslv);
            v.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_handle_section1));
            v.getBackground().setLevel(10000);
            return v;
        }
        
        @Override
        public void onDragFloatView(View floatView, Point position, Point touch) {

        }
        
        @Override
        public void onDestroyFloatView(View floatView) {
            
        }

    }
}
